using System;
using System.Collections;
using System.Collections.Generic;
using NuoYan.Extension;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;

public static class TextExtension
{
    public enum MaskType
    {
        TextColor,
        BackgroundColor
    }
    /// <summary>
    /// 当前鼠标位置下的文字的执行行为
    /// </summary>
    /// <param name="text"></param>
    /// <param name="camera"></param>
    /// <param name="action"></param> <summary>
    public static void LinkAction(this TMP_Text text, Camera camera, UnityAction<TMP_LinkInfo> action)
    {
        int linkIndex = TMP_TextUtilities.FindIntersectingLink(text, Input.mousePosition, camera);
        if (linkIndex == -1) return;
        TMP_LinkInfo linkInfo = text.textInfo.linkInfo[linkIndex];
        action?.Invoke(linkInfo);
    }
    /// <summary>
    /// 当前鼠标位置下的文字的执行行为 并改变链接文字颜色 
    /// </summary>
    /// <param name="text"></param>
    /// <param name="camera"></param>
    /// <param name="color"></param>
    /// <param name="action"></param>
    public static void LinkAction(this TMP_Text text, Camera camera, Color color, UnityAction<TMP_LinkInfo> action)
    {
        int linkIndex = TMP_TextUtilities.FindIntersectingLink(text, Input.mousePosition, camera);
        if (linkIndex == -1) return;
        TMP_LinkInfo linkInfo = text.textInfo.linkInfo[linkIndex];
        var linkText = linkInfo.GetLinkText();
        text.text = text.text.ClearStringColor();
        text.text = text.text.Replace(linkText, linkText.ColorString(color));
        action?.Invoke(linkInfo);
    }
    [Obsolete("背景覆盖有问题，暂弃用")]
    public static void LinkAction(this TMP_Text text, Camera camera, MaskType type,Color color, UnityAction<TMP_LinkInfo> action)
    {
        int linkIndex = TMP_TextUtilities.FindIntersectingLink(text, Input.mousePosition, camera);
        if (linkIndex == -1) return;
        switch (type)
        {
            case MaskType.TextColor:
                LinkAction(text, camera, color, action);
                break;
            case MaskType.BackgroundColor:
                ApplyBackgroundMask(text, linkIndex, color);
                action?.Invoke(text.textInfo.linkInfo[linkIndex]);
                break;
        }
    }

    // 为链接文本添加背景遮罩效果
    public static void ApplyBackgroundMask(this TMP_Text textMeshPro, int linkIndex, Color maskColor)
    {
        RemoveBackgroundMask(textMeshPro);

        TMP_LinkInfo linkInfo = textMeshPro.textInfo.linkInfo[linkIndex];
        TMP_TextInfo textInfo = textMeshPro.textInfo;

        // 获取链接文本的边界
        float minX = float.MaxValue;
        float maxX = float.MinValue;
        float minY = float.MaxValue;
        float maxY = float.MinValue;

        // 找到链接文本的边界范围
        for (int i = 0; i < linkInfo.linkTextLength; i++)
        {
            int characterIndex = linkInfo.linkTextfirstCharacterIndex + i;
            if (characterIndex >= textInfo.characterInfo.Length)
                continue;
                
            TMP_CharacterInfo charInfo = textInfo.characterInfo[characterIndex];
            
            // 只处理可见字符
            if (!charInfo.isVisible)
                continue;

            Vector3 bottomLeft = charInfo.bottomLeft;
            Vector3 topRight = charInfo.topRight;
            

            minX = Mathf.Min(minX, bottomLeft.x);
            maxX = Mathf.Max(maxX, topRight.x);
            minY = Mathf.Min(minY, bottomLeft.y);
            maxY = Mathf.Max(maxY, topRight.y);
        }


        // 创建一个临时的背景对象
        GameObject background = new GameObject("LinkBackground");
        background.transform.SetParent(textMeshPro.transform);
        background.transform.localPosition = Vector3.zero;
        background.transform.localRotation = Quaternion.identity;
        background.transform.localScale = Vector3.one;

        // 添加背景图形
        UnityEngine.UI.Image image = background.AddComponent<UnityEngine.UI.Image>();
        image.color = maskColor;

        RectTransform rect = background.GetComponent<RectTransform>();
        rect.anchorMin = new Vector2(1f, 1f);
        rect.anchorMax = new Vector2(1f, 1f);
        rect.pivot = new Vector2(1f, 1f);

        // 设置背景大小和位置
        rect.sizeDelta = new Vector2(maxX - minX, maxY - minY);
        rect.anchoredPosition = new Vector2(minX, minY);

        // 确保背景在文本后面
        background.transform.SetSiblingIndex(0);

        // 存储背景引用以便后续删除
        textMeshPro.gameObject.AddComponent<LinkBackgroundTracker>().backgroundObject = background;
    }

    // 移除背景遮罩
    public static void RemoveBackgroundMask(this TMP_Text textMeshPro)
    {
        LinkBackgroundTracker tracker = textMeshPro.GetComponent<LinkBackgroundTracker>();
        if (tracker != null && tracker.backgroundObject != null)
        {
            UnityEngine.Object.Destroy(tracker.backgroundObject);
            UnityEngine.Object.Destroy(tracker);
        }
    }
}
